package com.icangya.admin;

import com.alibaba.fastjson.JSONObject;
import com.icangya.Utils.JSONUtils;
import com.icangya.service.ICSQRManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

@CrossOrigin
@RestController
@RequestMapping(value="/csqr")
public class CSQRController {
    @Autowired
    private ICSQRManageService icsqrManageService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public JSONObject getCSQR(){
       String res = icsqrManageService.getCSQR();
       if(res.equals("")){
           return JSONUtils.getRoot(-1,"还未上传客服二维码","");

       }
       return JSONUtils.getRoot(0,"success",res);
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public void uploadCSQR(@RequestParam(value = "file",required = false)MultipartFile file, HttpServletRequest request){
        if(file==null){return;}
        icsqrManageService.uploadCSQR(file,request);
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public JSONObject saveTheone(HttpServletRequest request){
        Serializable res=icsqrManageService.setCSQR(request);
        if (res.equals(0)){
            return JSONUtils.getRoot(-1,"falied","");
        }
        return JSONUtils.getRoot(0,"success","");
    }

}
